import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  Req,
  Request,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOkResponse,
  ApiOperation,
  ApiParam,
  ApiProperty,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { Public } from '../../common/decorators/public.decorator';
import { AuthService } from '../../core/auth/auth.service';
import { UserService } from './user.service';
import {
  PageSearchUserDto,
  UpdatePasswordDto,
  UserRegisterDto,
} from '../../dto/user/user-register.dto';
import { User } from '../../entities/user.entity';
import { PaginatedResponseDto } from '../../common/class/res.class';
import { DictService } from '../dict/dict.service';
import { RoleController } from '../role/role.controller';
import { RoleService } from '../role/role.service';

@ApiTags('用户管理')
// 这个很重要，没有他，swagger请求头不会带token发送请求
@ApiBearerAuth()
@Controller('user')
export class UserController {
  constructor(
    private readonly authService: AuthService,
    private readonly userService: UserService,
    private readonly dictService: DictService,
    private readonly roleService: RoleService,
  ) {}

  @ApiOperation({ summary: '测试token是否通过获取用户信息' })
  @Get('profile')
  getProfile(@Req() req) {
    // console.log("profile");
    // 获取到用户的token中的内容
    return req.user;
  }
  @Get('getUserInfo')
  @ApiOperation({ summary: '获取用户信息' })
  async getUserInfo(@Req() req): Promise<any> {
    const userInfo = await this.userService.getUserInfo(req.user);
    const Dict = await this.dictService.getDictConfig();
    delete userInfo.password;
    return {
      code: 200,
      msg: '查询成功',
      data: { userInfo, sysAllDictItems: Dict },
    };
  }

  @Get('permission/getUserPermissionByToken')
  @ApiOperation({ summary: '获取用户权限菜单' })
  async getUserPermissionByToken(@Req() req): Promise<any> {
    const [authMenu] = await this.roleService.findMenusByRoleId(
      req.user.rolesIds,
    );
    if (!authMenu) {
      return {
        code: 200,
        msg: '当前无关联角色，请联系管理员分配',
        data: null,
      };
    }
    return {
      code: 200,
      msg: '查询成功',
      data: { menu: authMenu },
    };
  }

  @Public()
  @ApiOperation({ summary: '注册用户(不用携带token)' })
  @ApiBody({ type: UserRegisterDto, description: '输入用户的各种信息' })
  @Post('register')
  async register(@Body() user: UserRegisterDto): Promise<any> {
    await this.userService.register(user);
    return '注册成功';
  }

  @ApiOperation({ summary: '修改密码' })
  @ApiBody({ type: UserRegisterDto, description: '输入用户的各种信息' })
  @Post('changePassword')
  async changePassword(@Body() user: UpdatePasswordDto): Promise<any> {
    await this.userService.updatePassword(user);
    return '修改密码成功';
  }

  @ApiOperation({ summary: '新增成功' })
  @ApiBody({ type: UserRegisterDto, description: '输入用户的各种信息' })
  @Post('add')
  async add(@Body() user: UserRegisterDto): Promise<any> {
    await this.userService.register(user);
    return ' 新增成功';
  }

  @ApiOperation({ summary: '分页条件查询角色信息' })
  @ApiOkResponse({ type: PageSearchUserDto, description: '分页查询结果' })
  @Get('page')
  async page(
    @Query() dto: PageSearchUserDto,
  ): Promise<PaginatedResponseDto<Partial<User>>> {
    const [records, total] = await this.userService.page(dto);
    return {
      msg: '查询成功',
      data: {
        records,
        pageSize: dto.pageSize,
        pageNo: dto.pageNo,
        total,
      },
    };
  }

  @ApiOperation({ summary: '通过id删除用户' })
  @ApiParam({ name: 'id', required: true, description: '删除用户的id' })
  @Delete('delete')
  async remove(@Body() obj: any): Promise<any> {
    await this.userService.remove(obj.id);

    return '删除成功';
  }

  @ApiOperation({ summary: '通过用户id更新用户信息' })
  @ApiBody({ type: UserRegisterDto, description: '输入更改的信息' })
  @Post('edit')
  async update(@Body() updateInput: UserRegisterDto): Promise<any> {
    const data = await this.userService.update(updateInput);
    return data;
  }

  @ApiOperation({ summary: '查询单个用户和用户关联的文章' })
  @ApiParam({ name: 'id', required: true, description: '想要查询的id' })
  @Get(':id')
  async findOne(@Param() id: string): Promise<any> {
    const data = await this.userService.findOneWithPostsById(id);
    return data;
  }

  @ApiOperation({ summary: '查询所有用户' })
  @Get()
  async findAll(): Promise<any> {
    const data = await this.userService.findAll();
    return data;
  }
}
